package com.pdstudio.carrecom.ui.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.pdstudio.carrecom.R;

public class SlideMenuView extends LinearLayout {
	Context _context;
	AQuery mAquery;
	List<String> _datas = new ArrayList<String>();
	List<Integer> _icons = new ArrayList<Integer>();
	MenuAdapter _adapter;
	// OnItemClickListener _listListener;
	ListView _list;

	public SlideMenuView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initView(context);

	}

	public SlideMenuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public void setData(String[] datas, Integer[] listIcon) {
		setData(Arrays.asList(datas));
		_icons = Arrays.asList(listIcon);
	}

	public void setData(List<String> datas) {
		_datas = datas;
		if (_adapter != null) {
			_adapter.notifyDataSetChanged();
		}
	}

	public void setOnItemClicked(OnItemClickListener listListener) {
		_list.setOnItemClickListener(listListener);
	}

	public void setOnItemClicked(List<String> datas) {
		_datas = datas;
		if (_adapter != null) {
			_adapter.notifyDataSetChanged();
		}
	}

	private void initView(Context context) {
		_context = context;
		_icons = new ArrayList<Integer>();

		View v = LayoutInflater.from(context).inflate(
				R.layout.sliding_menu_main, null);
		mAquery = new AQuery(v);

		_list = (ListView) v.findViewById(R.id.menu_list);
		_adapter = new MenuAdapter();
		_list.setAdapter(_adapter);

		addView(v);

	}

	class MenuAdapter extends BaseAdapter {

		public MenuAdapter() {

		}

		@Override
		public int getCount() {
			return _datas.size();
		}

		@Override
		public String getItem(int position) {
			// TODO Auto-generated method stub
			return _datas.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(_context).inflate(
						R.layout.list_item_menu, parent,false);
			}

			String data = getItem(position);
			int icon = _icons.get(position);
			if (data != null) {
				TextView tv = (TextView) convertView
						.findViewById(R.id.menu_tv_text);
				tv.setText(data);

				ImageView iv = (ImageView) convertView
						.findViewById(R.id.menu_iv_pic);
				iv.setImageResource(icon);

			}

			return convertView;

		}

	}

}
